package com.mutual.controller.dividend;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.json.simple.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.mutual.base.controller.BaseController;
import com.mutual.base.form.DividendForm;
import com.mutual.entity.Dividend;
import com.mutual.service.IDividendService;



/**
 * 分红表
 * @creator     中城智库
 * @create-time 2016/11/3 15:55:37
 * @company www.myzczk.com
 * @version 1.0
 */
@Controller("dividendController")
@Scope("prototype")
@RequestMapping("/dividend")
public class DividendController extends BaseController {

	private Logger logger = Logger.getLogger(DividendController.class);
	
	@Resource(name = "dividendServiceImpl")
	private IDividendService dividendService;
	


	/**
	 * 首页
	 * @return 
	 */
	@RequestMapping(value = "/index.htm", method = RequestMethod.GET)
	public String index(){
		return "/dividend/dividend_index";
	}

	/**
	 * 分页查询
	 * @return 
	 */
	@RequestMapping(value = "/listDividendByPage.htm", method = RequestMethod.POST)
	public void listDividendByPage(@ModelAttribute DividendForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try{
			dividendService.listDividendByPage(model.getPageResult(),model.getCondition());
			JSONObject root = toPageJson(model.getPageResult(), new String[]{"id","totalNumber","investNumber","dayDividend","maintain","totalStock","stockNumber","outStock","incomeTime","memberId","status"});
			ajaxPageResult(root,response);
		}catch(Exception ex){
			String msg = "查询分红表时发生异常："+ex.getMessage();
			logger.error(msg,ex);
		}
	}

	/**
	 * 保存分红表
	 * @return 
	 */
	@RequestMapping(value = "/edit.htm", method = RequestMethod.GET)
	public ModelAndView edit(@ModelAttribute DividendForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		ModelAndView view = new ModelAndView();
		view.setViewName("/dividend/dividend_add");
		Map<String,Object> data = view.getModel();
		if(StringUtils.isNotBlank(model.getCondition().getId())){
		Dividend item = dividendService.getDividendById(model.getCondition().getId());
			data.put("item", item);
		}
		


		return view;
	}

	/**
	 * 保存分红表
	 * @return 
	 */
	@RequestMapping(value = "/saveDividend.htm", method = RequestMethod.POST)
	public ModelAndView saveDividend(@ModelAttribute DividendForm model,HttpServletRequest request,HttpSession session){
		try {
			if(StringUtils.isBlank(model.getCondition().getId())){
			dividendService.add(model.getCondition());
			}else{
				dividendService.update(model.getCondition());
			}
			return ajaxJSON(Status.success,"保存成功");
		} catch (Exception e) {
			String msg = "保存分红表时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "保存失败，请稍后重试");
		}
	}
	/**
	 * 删除分红表
	 * @return 
	 */
	@RequestMapping(value = "/del.htm", method = RequestMethod.POST)
	public ModelAndView del(DividendForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try {
			dividendService.delByIds(model.getSelIds().split(","));
			return ajaxJSON(Status.success,"删除成功");
		} catch (Exception e) {
			String msg = "删除分红表时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "删除失败，请稍后重试");
		}
	}

}